import cn.microven.mindmap.components.control.NodeContextMenu;
import cn.microven.mindmap.struture.MindMap;
import cn.microven.mindmap.utils.ComponentGetter;
import cn.microven.mindmap.utils.ControllerGetter;
import javafx.application.Application;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.geometry.Insets;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TabPane;
import javafx.scene.control.TreeView;
import javafx.scene.control.cell.TextFieldTreeCell;
import javafx.scene.image.Image;
import javafx.scene.layout.*;
import javafx.scene.paint.Paint;
import javafx.stage.Stage;
import javafx.util.StringConverter;

import java.io.IOException;
import java.net.URL;
import java.time.Duration;
import java.time.Instant;
import java.util.ResourceBundle;

public class App extends Application implements Initializable {

    @FXML
    public ScrollPane main;

    public TreeView right;

    @FXML
    public BorderPane root;


    public Scene scene;

    //右侧栏宽度
    public final static int RIGHT_WIDTH = 350;

    public TabPane menuTab;

    public ScrollPane menuScroll;

    public HBox menuWrapper;

    public HBox fileMenuWrapper;

    public static void main(String[] args) {
        Application.launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("思维导图");
        //设置缩放的最小宽度
        primaryStage.setMinHeight(600);
        primaryStage.setMinWidth(600);

        primaryStage.getIcons().add(new Image("http://score.scaumis.cn/app.png"));

        Parent root = null;
        try {
            root = FXMLLoader.load(getClass().getResource("cn/microven/mindmap/view/layout.fxml"));
            //通过root新建一个scene

            scene = new Scene(root);
            scene.getStylesheets().add(NodeContextMenu.class.getResource("NodeContextMenu.css").toExternalForm());
            primaryStage.setScene(scene);

            primaryStage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void initialize(URL url, ResourceBundle resourceBundle) {
        //窗口大小变化监听器
        root.widthProperty().addListener((observableValue, number, t1) -> resizeWidth((Double)t1));
        main.widthProperty().addListener((observableValue, number, t1) -> resizeWidth(root.getWidth()));
        main.heightProperty().addListener((observableValue, number, t1) -> resizeWidth(root.getWidth()));

        MindMap map = null;
        try {
            map = MindMap.initInstance();

            main.setContent(map);

            TreeView treeView = new TreeView<>(map.getRoot().getTreeItem());
            treeView.setPrefWidth(RIGHT_WIDTH-2);
            // 设置为可编辑的
            treeView.setEditable(true);
            treeView.setCellFactory(TextFieldTreeCell.forTreeView(new StringConverter<String>() {
                @Override
                public String toString(String s) {
                    return s;
                }
                @Override
                public String fromString(String s) {
                    return s;
                }
            }));
            this.right = treeView;
            root.setCenter(treeView);

            main.setOnMouseClicked((mouseEvent -> {
                if (Duration.between(MindMap.getInstance().getSelectedNodeChangeTime(), Instant.now() ).toMillis() > 300) {
                    MindMap.getInstance().clearSelect();
                }
            }));

            ComponentGetter.setMapWrapper(main);
            ComponentGetter.setTreeView(right);
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {

            FXMLLoader newFileMenu = new FXMLLoader(getClass().getResource("cn/microven/mindmap/view/NewFileMenuBar.fxml"));
            menuWrapper.getChildren().add(newFileMenu.load());
            ControllerGetter.setNewFileMenu(newFileMenu.getController());

            FXMLLoader fontStyleMenu = new FXMLLoader(getClass().getResource("cn/microven/mindmap/view/FontStyleMenuBar.fxml"));
            menuWrapper.getChildren().add(fontStyleMenu.load());
            ControllerGetter.setFontStyleMenu(fontStyleMenu.getController());

            FXMLLoader nodeStyleMenu = new FXMLLoader(getClass().getResource("cn/microven/mindmap/view/NodeStyleMenuBar.fxml"));
            menuWrapper.getChildren().add(nodeStyleMenu.load());
            ControllerGetter.setNodeStyleMenu(nodeStyleMenu.getController());

            FXMLLoader layoutMenu = new FXMLLoader(getClass().getResource("cn/microven/mindmap/view/LayoutMenuBar.fxml"));
            menuWrapper.getChildren().add(layoutMenu.load());
            ControllerGetter.setLayoutMenu(layoutMenu.getController());

            FXMLLoader operationMenu = new FXMLLoader(getClass().getResource("cn/microven/mindmap/view/OperationMenuBar.fxml"));
            menuWrapper.getChildren().add(operationMenu.load());
            ControllerGetter.setOperationMenu(operationMenu.getController());

            FXMLLoader fileMenu = new FXMLLoader(getClass().getResource("cn/microven/mindmap/view/FileMenuBar.fxml"));
            fileMenuWrapper.getChildren().add(fileMenu.load());
            ControllerGetter.setFileMenu(fileMenu.getController());


        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 对布局中的元素大小进行调整
     *
     * @param rootWidth 根节点(BorderPane)的宽度
     */
    public void resizeWidth(Double rootWidth) {
        if (right.getWidth() != 0) {
            main.setPrefWidth(rootWidth -RIGHT_WIDTH);
        }
        menuTab.setMinWidth(rootWidth);
        menuScroll.setMaxWidth(rootWidth);
        main.setBackground(new Background(new BackgroundFill(Paint.valueOf("red"),new CornerRadii(10),new Insets(10))));
    }




}

